﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Summer.Exception;
using System.IO;
using Summer.Object;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.IO
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009-11-10 21:44:37
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.IO
{
    /// <summary>
    /// Define some Extension methods about File
    /// </summary>
    public static class FileManagement
    {
        /// <summary>
        /// Get Full path by filePath
        /// </summary>
        /// <param name="filePath">file path with the full name of file</param>
        /// <param name="e">The exception thrown if load failure</param>
        /// <returns></returns>
        public static string FindPath(this string filePath, AbstractSummerException e)
        {
            string path = Find(filePath, null);
            if (path.Trim() == string.Empty)
            {
                //throw exception
                throw e;
            }
            return path;
        }
        /// <summary>
        /// Get Full path by filePath and other append path
        /// </summary>
        /// <param name="filePath">file path with the full name of file</param>
        /// <param name="otherPath"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string FindPath(this string filePath,string[] otherPaths, AbstractSummerException e)
        {
            string path = Find(filePath, otherPaths);
            if (path.Trim() == string.Empty)
            {
                //throw exception
                throw e;
            }
            return path;
        }
        private static string Find(string filePath, string[] otherPaths)
        {
            string fileName = new FileInfo(filePath).Name;
            //Current path
            if (File.Exists(filePath))
            {
                return filePath;
            }
            //Current path with bin
            if (File.Exists(Path.Combine(Const.BIN, filePath)))
            {
                return Path.Combine(Const.BIN, filePath);
            }
            //Full path by current path
            if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath)))
            {
                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath);
            }
            //Full path by current path with bin
            if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine(Const.BIN, filePath))))
            {
                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine(Const.BIN, filePath));
            }
            //Other path to find
            if (otherPaths != null && otherPaths.Length != 0)
            {
                foreach (string s in otherPaths)
                {
                    string p = Path.Combine(s, fileName);
                    if (File.Exists(p))
                    {
                        return p;
                    }
                }
            }
            return string.Empty;
        }
    }
}
